﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
namespace ChartEdit
{
    public class ClickData
    {
        public ClickData()
        {
            for (int i = 0; i < ContainsType.Length; i++)
                ContainsType[i] = false;
            Notes.Clear();
            SpecialNotes.Clear();
        }
        //single object stuff
        public ObjectType OT;
        public int Index;

        //selections
        public bool[] ContainsType = new bool[Enum.GetValues(typeof(ObjectType)).Length];
        public List<RendererNote> Notes = new List<RendererNote>();
        public List<RendererNote> SpecialNotes = new List<RendererNote>();
    }
    public enum ObjectType
    {
        FretboardLeft,
        FretboardRight,
        Fretboard,
        Note,
        SustainTop,
        SpecialBottom,
        SpecialTop,
        Special,
        GlobalEvent,
        TrackEvent,
        SyncEvent,
        Anchor,
        BPM,
        Nothing,
    }
    public class MouseManager
    {
        public MouseManager(Editor ed)
        {
            this.ed = ed;
            this.pr = ed.PR;
        }

        private Editor ed;
        private PreRenderer pr;
        public int fat=8;


        #region Collision Detection
        private bool intersectsV(int mX, int X)
        {
            if (Math.Abs(mX - X) < fat)
                return true;
            return false;
        }
        private bool intersectsV(float mX, float X)
        {
            if (Math.Abs(mX - X) < (float)fat)
                return true;
            return false;
        }
        private bool intersectsH(int mY, int Y)
        {
            if (Math.Abs(mY - Y) < fat)
                return true;
            return false;
        }
        private bool intersectsH(float mY, float Y)
        {
            if (Math.Abs(mY - Y) < (float)fat)
                return true;
            return false;
        }
        private bool intersectsCircle(float x1, float y1, float x2, float y2, float r)
        {
            float x = x2 - x1;
            float y = y2 - y1;
            if (Math.Sqrt(x * x + y * y) < r + (float)fat)
                return true;
            return false;
        }
        private bool intersectsRect(float x, float y, RectangleF rectF)
        {
            if (x > rectF.Left & x < rectF.Right & y > rectF.Top & y < rectF.Bottom)
                return true;
            return false;
        } 
        #endregion
        /*
        public ClickData GetElement(int mX, int mY)
        {
            ObjectType ot;
            switch (ot)
            {
                case(ObjectType.FretboardLeft):
                case(ObjectType.FretboardRight):
                case(ObjectType.Fretboard):
                case(ObjectType.Nothing):
                    return new ClickData(ot);
                case(ObjectType.Note):
                    break;
            }
        }
        */
        private Note getNote(int x, int y)
        {
            return null;
        }

        public int GetFret(float x)
        {
            int dist = Int32.MaxValue;
            int fret = -1;

            for (int i=0;i<ed.currentTheme.Elements.Count();i++)
            {
                float x2 = ed.FbXB + ed.currentTheme[i].Offset.X * ed.FbWidthB;
                if (Math.Abs(x2 - x) < dist)
                {
                    dist = (int)Math.Abs(x2 - x);
                    fret = i;
                }
            }

            return fret;
        }

        public ClickData GetClickData(int mX, int mY)
        {
            float x = mX;
            float y = mY;
            RectangleF box=new RectangleF();
            ClickData CData = new ClickData();

            //Notes
            foreach (RendererNoteList rnl in pr.NoteLists)
                foreach (RendererNote rn in rnl)
                {
                    box.X = rn.XYf.X + rn.BBox.X;
                    box.Y = rn.SustainXYf.Y + rn.BBox.Y;
                    box.Width = rn.BBox.Width;
                    box.Height = rn.BBox.Height + (rn.XYf.Y - rn.SustainXYf.Y);
                    if (intersectsRect(x, y, box))
                        CData.Notes.Add(rn);
                }
            
            if (CData.Notes.Count() > 0)
                CData.ContainsType[(byte)ObjectType.Note] = true;
            //Fretboard Left Edge
            if (intersectsV(x, ed.FbX))
                CData.ContainsType[(byte)ObjectType.FretboardLeft] = true;
            
            //Fretboard Right Edge
            if (intersectsV(x, ed.FbXR))
                CData.ContainsType[(byte)ObjectType.FretboardRight] = true;

            return CData;

        }

        public ObjectType GetObjectType(int mX, int mY)
        {
            //Higher priority at the top
            float x = (float)mX;
            float y = (float)mY;

            if (intersectsV(x, ed.FbX))
                return ObjectType.FretboardLeft;
            if (intersectsV(x, ed.FbXR))
                return ObjectType.FretboardRight;

            return ObjectType.Nothing;
        }



    }
}
